﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;


using Visio = Microsoft.Office.Interop.Visio;
//using Interop = Microsoft.Office.Interop;
//using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication2
{
    static class Program
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string _DocumentName = string.Empty;

            Application.Run(new MainForm());





        }
    }
    static class Function
    {
        public static Rack myRack;

        public static void setRack(Rack newRack)
        {
            myRack = newRack;
        }
        public static Rack getRack()
        {
            return myRack;
        }

    }

    class Rack
    {
        public int isRack { get; set; }
        public string rackName { get; set; }
        public int rackHeight { get; set; }
        public int rackID { get; set; }
        public int power { get; set; }
        public int btu { get; set; }
        public int amp { get; set; }
        private List<Item> _Items = new List<Item>();
        public int rackCount { get; set; }
        public List<Item> Items
        {
            get { return _Items; }
        }

        public Rack() { }
        public Rack(string newname, int newheight, int newid, int newisrack, int newpower, int newbtu, int newamp)
        {
            rackName = newname;
            rackHeight = newheight;
            rackID = newid;
            isRack = newisrack;
            power = newpower;
            btu = newbtu;
            amp = newamp;
            rackCount++;
        }


    }
    class Item
    {

        public int itemID { get; set; }
        public int itemCount { get; set; }
        public string serialNumber { get; set; }
        public int x { get; set; }
        public int y { get; set; }
        public int width { get; set; }
        public int height { get; set; }
        public string modelNumber { get; set; }
        public string modelName { get; set; }
        public string manufacturer { get; set; }
        public int manId { get; set; }
        public string description { get; set; }
        public string barcode { get; set; }
        public string visioStencil { get; set; }
        public int visioShapeId { get; set; }
        public int power { get; set; }
        public int btu { get; set; }
        public string rackname { get; set; }
        public int rackid { get; set; }
        public int amp { get; set; }
        public double xoff { get; set; }
        public double yoff { get; set; }


        public Item() { }
        public Item(int id, int xval, int yval, int newwidth, int newheight, 
            string newsn, string newmodelnumber, string newmanufacturer, 
            string newdescription, string newbarcode, int newmanid, string newstencil, 
            int newshapeid, int newpower, int newbtu, string newrackname, int newrackid, int newamp,
            double newxoffset, double newyoffset)
        {
            serialNumber = newsn;
            x = xval;
            y = yval;
            width = newwidth;
            height = newheight;
            itemID = id;
            modelNumber = newmodelnumber;
            manId = newmanid;
            manufacturer = newmanufacturer;
            description = newdescription;
            barcode = newbarcode;
            visioShapeId = newshapeid;
            visioStencil = newstencil;
            power = newpower;
            btu = newbtu;
            rackname = newrackname;
            rackid = newrackid;
            amp = newamp;
            xoff = newxoffset;
            yoff = newyoffset;
            itemCount++;
        }
    }
    public class TextOnlyShapeCreation
    {

        /// <summary>This constructor is intentionally left blank.</summary>
        public TextOnlyShapeCreation()
        {

            // No initialization is required.
        }

        /// <summary>This method draws a rectangle and sets the appropriate
        /// properties of the shape to make it a text-only shape.</summary>
        /// <param name="targetPage">Reference to the page that will be used
        /// </param>
        /// <param name="shapeText">Text to be set in the text-only shape
        /// </param>
        /// <returns>Text-only shape that is created, otherwise null</returns>
        /// 
        public static void printlabel(Visio.Page targetPage, Visio.Shape origshape, string shapeText, string offset)
        {
            Visio.Shape newtextshape;
            offset = offset.ToUpper();
            double x1, x2, y1, y2;
            double orgx1, orgy1;
            const string styleName = "Text Only";



            orgx1 = origshape.get_CellsSRC(
                (short)Visio.VisSectionIndices.visSectionObject,
                (short)Visio.VisRowIndices.visRowXFormOut,
                (short)Visio.VisCellIndices.visXFormPinX)
                .get_Result((short)Visio.VisUnitCodes.visNoCast);
            orgy1 = origshape.get_CellsSRC(
                (short)Visio.VisSectionIndices.visSectionObject,
                (short)Visio.VisRowIndices.visRowXFormOut,
                (short)Visio.VisCellIndices.visXFormPinY)
                .get_Result((short)Visio.VisUnitCodes.visNoCast);
            switch (offset)
            {

                case "UP":
                    x1 = orgx1 - 2;
                    y1 = orgy1 + 2;
                    x2 = x1 + 2;
                    y2 = y1 + 1;
                    break;
                case "DOWN":
                    x1 = orgx1 - 2;
                    y1 = orgy1 - 1;
                    x2 = x1 + 2;
                    y2 = y1 + 1;
                    break;

                default:
                    x1 = y1 = x2 = y2 = orgx1;
                    break;
            }

            newtextshape = targetPage.DrawRectangle(x1, y1, x2, y2);

            // Set the LineStyle and FillStyle properties of the shape.  The
            // Text Only style is one of the Visio default document styles.
            newtextshape.LineStyle = styleName;
            newtextshape.FillStyle = styleName;
            



            // Set the shape text with the text passed in.
            newtextshape.Text = shapeText;
            newtextshape.Characters.set_CharProps((short)Microsoft.Office.Interop.Visio.
                        VisCellIndices.visCharacterSize,
                    10);
            
        }
        
        public static void printlabel(Visio.Page targetPage, string shapeText, string offset,int h, int w, int x, int y)
        {
            Visio.Shape newtextshape;
            offset = offset.ToUpper();
            double x1, x2, y1, y2;
            double orgx1, orgy1;
            const string styleName = "Text Only";



            
            switch (offset)
            {

                case "RIGHT":
                    x1 = 3;
                    x2 = 5;
                    if (h == 1) 
                    { 
                        if (w == 1) 
                        { 
                            y1 = U2ft(y + h) + 2; 
                            y2 = U2ft(y + h + 1) + 2; 
                        } 
                        else
                        {
                            y1 = U2ft(y + h + 1/x - 1) + 2;
                            y2 = U2ft(y + h + 1/x) + 2; 
                        }
                    }
                    else
                    {
                        if (w == 1) 
                        {
                            y1 = U2ft(y + h - 1 ) + 2;
                            y2 = U2ft(y + h) + 2; 
                        }
                        else
                        {
                            y1 = U2ft(y) + 2 - (x - 1) *.1;
                            y2 = U2ft(y + h + 1) + 2 - (x - 1) * .1; 
                        }
                    }
                    break;

                default:
                    x1 = y1 = x2 = y2 = x;
                    break;
            }

            newtextshape = targetPage.DrawRectangle(x1, y1, x2, y2);

            // Set the LineStyle and FillStyle properties of the shape.  The
            // Text Only style is one of the Visio default document styles.
            newtextshape.LineStyle = styleName;
            newtextshape.FillStyle = styleName;
            



            // Set the shape text with the text passed in.
            newtextshape.Text = shapeText;
            newtextshape.Characters.set_CharProps((short)Microsoft.Office.Interop.Visio.
                        VisCellIndices.visCharacterSize,
                    6);
            
            
        }
        public static double U2ft(int num)
        {
            double temp;
            temp = (num * .146);
            return temp;
        }
        public static Microsoft.Office.Interop.Visio.Shape DemoTextOnlyShapeCreation(
            Microsoft.Office.Interop.Visio.Page targetPage,
            string shapeText)
        {

            if (targetPage == null)
            {
                return null;
            }

            const string styleName = "Text Only";

            Microsoft.Office.Interop.Visio.Shape textOnlyShape = null;
            

            try
            {

                // Draw a rectangle shape.
                textOnlyShape = targetPage.DrawRectangle(2, 9, 6.5, 8);

                // Set the LineStyle and FillStyle properties of the shape.  The
                // Text Only style is one of the Visio default document styles.
                textOnlyShape.LineStyle = styleName;
                textOnlyShape.FillStyle = styleName;

                // Set the shape text with the text passed in.
                textOnlyShape.Text = shapeText;
                

            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                throw;
            }

            return textOnlyShape;
        }
    }
    public class ShapeSearchWindow
    {

        /// <summary>This method is the class constructor.</summary>
        public ShapeSearchWindow()
        {

            // No initialization is required.
        }

        /// <summary>This method sets the visibility of the shape search window.
        /// </summary>
        /// <param name="targetWindow">Reference to the window that will contain
        /// the shape search window.</param>
        /// <param name="visible">Show or hide the shape search window.</param>
        public static void SetVisible(
            Microsoft.Office.Interop.Visio.Window targetWindow,
            bool visible)
        {

            try
            {

                // Verify that the window object is valid
                if (targetWindow == null)
                {

                    throw new System.ArgumentNullException("targetWindow",
                        "Null object input.");
                }

                Microsoft.Office.Interop.Visio.Window searchWindow;
                searchWindow = targetWindow.Windows.get_ItemFromID(
                    System.Convert.ToInt16(Microsoft.Office.Interop.Visio.
                        VisWinTypes.visWinIDShapeSearch,
                    System.Globalization.CultureInfo.CurrentUICulture));

                if (searchWindow != null)
                {
                    searchWindow.Visible = visible;
                }
            }

            catch (Exception error)
            {
                System.Diagnostics.Debug.WriteLine(error.Message);
                throw;
            }
        }
    }


    /// <summary>This class demonstrates how to iterate through the
    /// Shape Transform section (fixed cell names), Custom Properties section
    /// (insertable rows), and multiple Geometry sections of the ShapeSheet.
    /// </summary>
    public class CellsIterator
    {

        /// <summary>This constructor is intentionally left blank.</summary>
        public CellsIterator()
        {

            // No initialization is required.
        }

        /// <summary>This method calls the iterateSection method to iterate
        /// through all the cells in different sections of the ShapeSheet.
        /// </summary>
        /// <param name="targetShape">Reference to the shape that will be used
        /// </param>
        /// <returns>true if successful, otherwise false</returns>
        public static bool DemoIteratingCellsInShape(
            Microsoft.Office.Interop.Visio.Shape targetShape)
        {

            bool returnValue = false;

            try
            {

                // Iterate through the Shape Transform section.
                returnValue = iterateShapeTransform(targetShape);

                if (returnValue)
                {

                    // Iterate through the Custom Properties section.
                    returnValue = iterateCustomProperties(targetShape);
                }

                if (returnValue)
                {

                    // Iterate through each Geometry section.
                    returnValue = iterateGeometry(targetShape);
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                throw;
            }

            return returnValue;
        }

        /// <summary>This method iterates through the Shape Transform section.
        /// </summary>
        /// <param name="targetShape">Reference to the shape that will be used
        /// </param>
        /// <returns>true if successful, otherwise false</returns>
        public static bool iterateShapeTransform(
            Microsoft.Office.Interop.Visio.Shape targetShape)
        {

            short sectionIndex;
            bool returnValue = false;

            try
            {

                // Iterate through the Shape Transform section.
                sectionIndex = (short)Microsoft.Office.Interop.Visio.
                    VisSectionIndices.visSectionObject;
                System.Diagnostics.Debug.WriteLine(
                    "\r\nSHAPE TRANSFORM SECTION");
                returnValue = iterateSection(targetShape,
                    sectionIndex);
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                throw;
            }

            return returnValue;
        }

        /// <summary>This method iterates through the Custom Properties section.
        /// </summary>
        /// <param name="targetShape">Reference to the shape that will be used
        /// </param>
        /// <returns>true if successful, otherwise false</returns>
        public static bool iterateCustomProperties(
            Microsoft.Office.Interop.Visio.Shape targetShape)
        {

            short sectionIndex;
            bool returnValue = false;

            try
            {

                // Iterate through the Custom Properties section.
                sectionIndex = (short)Microsoft.Office.Interop.Visio.
                    VisSectionIndices.visSectionProp;

                // See if the Custom Properties section exists
                if (0 != targetShape.get_SectionExists(sectionIndex,
                    (short)Microsoft.Office.Interop.Visio.
                    VisExistsFlags.visExistsAnywhere))
                {

                    System.Diagnostics.Debug.WriteLine(
                        "\r\nCUSTOM PROPERTIES SECTION");
                    string tempstr = string.Format("Section Index: {0}", sectionIndex);
                    System.Diagnostics.Debug.WriteLine(tempstr);
                    returnValue = iterateSection(targetShape,
                        sectionIndex);
                }
                else
                {

                    returnValue = true;
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                throw;
            }

            return returnValue;
        }

        /// <summary>This method iterates through each Geometry section.
        /// </summary>
        /// <param name="targetShape">Reference to the shape that will be used
        /// </param>
        /// <returns>true if successful, otherwise false</returns>
        public static bool iterateGeometry(
            Microsoft.Office.Interop.Visio.Shape targetShape)
        {

            short sectionIndex;
            short sectionCount;
            bool returnValue = false;

            try
            {

                sectionCount = targetShape.GeometryCount;

                returnValue = true;

                // Iterate through each Geometry section.
                for (short geometrySection = 0;
                    geometrySection < sectionCount && returnValue;
                    geometrySection++)
                {

                    // Get the section index of the current Geometry
                    // section.
                    sectionIndex = (short)(
                        (short)Microsoft.Office.Interop.Visio.
                            VisSectionIndices.visSectionFirstComponent +
                        geometrySection);
                    System.Diagnostics.Debug.WriteLine(
                        "\r\nGEOMETRY SECTION");
                    returnValue = iterateSection(targetShape,
                        sectionIndex);
                }
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                throw;
            }

            return returnValue;
        }

        /// <summary>This method iterates through the section specified in
        /// the sectionIndex parameter for a given shape and displays the
        /// universal name, formula, and value for each cell in the debug
        /// window.</summary>
        /// <param name="targetShape">Reference to the shape that will be used
        /// </param>
        /// <param name="sectionIndex">Index of the section that is to be
        /// iterated</param>
        /// <returns>true if successful, otherwise false</returns>
        private static bool iterateSection(
            Microsoft.Office.Interop.Visio.Shape targetShape,
            short sectionIndex)
        {

            Microsoft.Office.Interop.Visio.Section currentSection;
            Microsoft.Office.Interop.Visio.Cell currentCell;
            short rowCount;
            short cellCount;
            string cellFormula;
            bool returnValue = false;

            try
            {

                // Get the row count for this section.
                currentSection = targetShape.get_Section(sectionIndex);
                rowCount = targetShape.get_RowCount(sectionIndex);

                // Iterate through each row in this section.
                for (short rowIndex = 0; rowIndex < rowCount; rowIndex++)
                {

                    cellCount = targetShape.get_RowsCellCount(sectionIndex,
                        rowIndex);
                    if (cellCount == 0)
                    {

                        cellCount = targetShape.get_RowsCellCount(sectionIndex,
                            ++rowIndex);
                    }

                    // Get each of the cells in this section.
                    for (short cellIndex = 0; cellIndex < cellCount;
                        cellIndex++)
                    {

                        currentCell = currentSection[rowIndex][cellIndex];
                        cellFormula = currentCell.FormulaU;

                        // Replace empty formula strings with "No Formula"
                        if (0 == cellFormula.Length)
                        {

                            cellFormula = "No Formula";
                        }

                        // Output the cell to the debug window
                        System.Diagnostics.Debug.WriteLine(currentCell.Name +
                            " - Formula: " + cellFormula +
                            ", Value: " +
                            currentCell.get_ResultStr(currentCell.Units));
                    }
                }

                returnValue = true;
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                //throw;
            }

            return returnValue;
        }
    }
    /// <summary>This class is used to set up a window to handle mouse events.
    /// </summary>
}



	





 


 







